package com.cw.jeeyt.core.impl.facadeExcel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.cw.jeeyt.api.dto.facadeExcel.ClassFileInfo;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.StringUtil;
import com.google.common.collect.Lists;

/**
 * 文件查找工具类
 *
 * @author chenw
 * @date 2019/3/27 18:15
 */
@Component
public class FileSearchCore {
    private static List<ClassFileInfo> fileList = new ArrayList<>();

    /**
     * 遍历目录下所有接口参数文件
     *
     * @param basePath
     * @return
     */
    public List<ClassFileInfo> getFileList(String baseClassPath , String basePath, String fileType, String filePrefix,
        String classPattern) {
        String[] filePatterns = filePrefix.split(",");
        List<String> filePatternList = Lists.newArrayList(filePatterns);
        if (!StringUtils.isEmpty(basePath)) {
            File dir = new File(basePath);
            // 该文件目录下文件全部放入数组
            File[] files = dir.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    String fileName = files[i].getName();
                    boolean exists = checkFilePatterns(fileName,filePatternList);
                    String path = files[i].getPath();
                    // 判断是文件还是文件夹
                    if (files[i].isDirectory()) {
                        // 获取文件绝对路径
                        getFileList(baseClassPath , files[i].getAbsolutePath(), fileType, filePrefix, classPattern);
                        // 判断文件名是否以 FileType结尾
                    } else if (fileName.endsWith(fileType) && exists && path.contains(
                        classPattern)) {
                        String packagePath = getPackagePath(baseClassPath,files[i].getPath(), files[i].getName());
                        String classCode = files[i].getName().replace(fileType, "");
                        boolean checkExist = checkClassInfo(fileList,classCode,packagePath);
                        if(checkExist){
                            continue;
                        }
                        ClassFileInfo classFileInfo = new ClassFileInfo();
                        classFileInfo.setClassCode(classCode);
                        classFileInfo.setClassName(files[i].getName());
                        classFileInfo.setPackagePath(packagePath);
                        classFileInfo.setClassFile(files[i]);
                        fileList.add(classFileInfo);
                    } else {
                        continue;
                    }
                }
            }
        }
        return fileList;
    }

    private boolean checkClassInfo(List<ClassFileInfo> fileList, String classCode, String packagePath) {
        for(ClassFileInfo classFileInfo : fileList){
            if(classFileInfo.getClassCode().equals(classCode) && classFileInfo.getPackagePath().equals(packagePath)){
                return true;
            }
        }
        return false;
    }

    private boolean checkFilePatterns(String fileName, List<String> filePatternList) {
        if(CollectionUtil.isEmpty(filePatternList) || StringUtil.isEmpty(fileName)){
            return false;
        }
        for(String filePattern : filePatternList){
            if(fileName.startsWith(filePattern) || fileName.endsWith(filePattern)){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取包路径
     */
    private String getPackagePath(String baseClassPath,String filePath, String fileName) {
        filePath = filePath.replace(fileName, "");
        filePath = filePath.replace(baseClassPath, "");
        filePath = filePath.replace("\\", ".");
        return filePath;
    }

    public static void main(String[] args) {
        // List<ClassFileInfo> fileInfos = getFileList("D:/yunrong/classes", ".class", "Enum", "com\\cw");
        // System.out.println(JSON.toJSONString(fileInfos.size() + ""));
        // System.out.println(JSON.toJSONString(fileInfos));
    }

    /**
     * 置空
     */
    public void emptyFileList() {
        fileList = new ArrayList<>();
    }
}
